package com.cyzy.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyzy.dto.Result;
import com.cyzy.dto.ResultCode;
import com.cyzy.mapper.BannerMapper;
import com.cyzy.pojo.Banner;
import com.cyzy.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public Result getBannerPage(Banner banner) {
        try {
            Page<Banner> page = new Page<>(banner.getCurrentPage(), banner.getPageSize());
            Page<Banner> resultPage = bannerMapper.selectBannerPage(page, banner);
            return Result.ok().data("page", resultPage);
        } catch (Exception e) {
            return Result.error().message("查询轮播图列表失败");
        }
    }

    @Override
    public Result addBanner(Banner banner) {
        try {
            // 根据类型清理不相关字段
            if (banner.getBannerType() != 0 && banner.getBannerType() != 1) {
                banner.setCourseId(null);
                banner.setCampusId(null);
            }

            if (banner.getBannerType() == 1) {
                banner.setCourseId(null);
            }

            int result = bannerMapper.insert(banner);
            return result > 0 ? Result.ok() : Result.error().message("添加轮播图失败");
        } catch (Exception e) {
            return Result.error().message("添加轮播图异常");
        }
    }

    @Override
    public Result updateBanner(Banner banner) {
        try {
//            if (banner.getBannerId() = null) {
//                return Result.error().message("轮播图ID不能为空");
//            }

            // 查询原记录，保留不可修改的字段
            Banner original = bannerMapper.selectById(banner.getBannerId());
            if (original == null) {
                return Result.error().message("轮播图不存在");
            }

            // 保持原有课程ID和校区ID不变
            banner.setCourseId(original.getCourseId());
            banner.setCampusId(original.getCampusId());

            int result = bannerMapper.updateById(banner);
            return result > 0 ? Result.ok() : Result.error().message("更新轮播图失败");
        } catch (Exception e) {
            return Result.error().message("更新轮播图异常");
        }
    }

    @Override
    public Result deleteBanner(Long bannerId) {
        try {
            int result = bannerMapper.deleteById(bannerId);
            return result > 0 ? Result.ok() : Result.error().message("删除轮播图失败");
        } catch (Exception e) {
            return Result.error().message("删除轮播图异常");
        }
    }

    @Override
    public Result changeBannerStatus(Long bannerId, Integer isVisible) {
        try {
            Banner banner = new Banner();
            banner.setBannerId(bannerId);
            banner.setIsVisible(isVisible);
            int result = bannerMapper.updateById(banner);
            return result > 0 ? Result.ok() : Result.error().message("状态更新失败");
        } catch (Exception e) {
            return Result.error().message("状态更新异常");
        }
    }
}